//
//  TopicViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "TopicViewController.h"

@interface TopicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITableView *newstTableView;
@property (weak, nonatomic) IBOutlet UITableView *hotTableView;
@property(nonatomic,strong) UISegmentedControl *seg;

@property(nonatomic,assign)NSInteger newStart;//最新列表数据起始位置
@property(nonatomic,assign)NSInteger newlimit;//最新列表的条数
@property(nonatomic,assign)NSInteger hotStart;//最热列表的起始位置
@property(nonatomic,assign)NSInteger hotLimit;//最热烈表的条数
@property(nonatomic,strong)NSMutableArray *newstDataSource;//最新列表数据源
@property(nonatomic,strong)NSMutableArray *hotDataSource;//最热烈表数据源

@property(nonatomic,strong)UIView *tableHeader;
@end

@implementation TopicViewController

- (NSMutableArray *)newstDataSource
{
    if (!_newstDataSource) {
        _newstDataSource = [NSMutableArray array] ;
    }
    return _newstDataSource ;
}

- (NSMutableArray *)hotDataSource
{
    if (!_hotDataSource) {
        _hotDataSource = [NSMutableArray array];
    }
    return _hotDataSource;
}
- (UISegmentedControl *)seg
{
    if (!_seg) {
        _seg = [[UISegmentedControl  alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        [_seg insertSegmentWithTitle:@"最热" atIndex:1 animated:YES];
        [_seg insertSegmentWithTitle:@"最新" atIndex:0 animated:YES];
     
        _seg.selectedSegmentIndex =0;

        //添加点击事件
        [_seg  addTarget:self action:@selector(clickSegementController:) forControlEvents:UIControlEventValueChanged];
    }
    return _seg;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.titleView = self.seg;

    [self.newstTableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"topic_cell"];
    [self.hotTableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"topic_cell"];
    [self loadNewstTableViewDataSourceWithStart:0 limit:10];
    [self loadHotTableViewDataSourceWithStart:0 limit:10];

#pragma mark ---------------上拉刷新，下拉加载-----------------------
    //下拉刷新
    self.newstTableView.mj_header = [MJRefreshStateHeader  headerWithRefreshingBlock:^{
        //刷新之前清空数组
        [self.newstDataSource removeAllObjects];
        self.newStart = 0;
        [self  loadNewstTableViewDataSourceWithStart:0 limit:10];
    }];
    
    //添加上拉加载,执行完之后要结束
    self.newstTableView.mj_footer = [MJRefreshBackGifFooter  footerWithRefreshingBlock:^{
        self.newStart += 10;
        [self  loadNewstTableViewDataSourceWithStart:self.newStart limit:10];
    }];
    
    /**********************      －＝ 华丽的分割线＝－           *********************/
    //下拉刷新
    self.hotTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        //刷新之前清空数组
        [self.hotDataSource removeAllObjects];
        self.hotStart = 0;
        [self  loadHotTableViewDataSourceWithStart:0 limit:10];
    }];
    
    //添加上拉加载,执行完之后要结束
    self.hotTableView.mj_footer = [MJRefreshBackGifFooter  footerWithRefreshingBlock:^{
        self.hotStart += 10;
        [self  loadHotTableViewDataSourceWithStart:self.newStart limit:10];
    }];


}
#pragma mark_----------数据请求------------

//加载最新列表的数据
- (void)loadNewstTableViewDataSourceWithStart:(NSInteger)start  limit:(NSInteger)limit {
    [RequestManager fetchDataWithURL:kTopicUrl parameters:@{@"start":@(start),@"limit":@(limit),@"sort":@"addtime",} method:@"POST" complet:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            TopicModel *model = [[TopicModel alloc] init];
            [model setValuesForKeysWithDictionary:smallDic];
            [model setValuesForKeysWithDictionary:smallDic[@"counterList"]];
            [self.newstDataSource  addObject:model];

        }
    } updataUI:^{
        //结束刷新
        [self.newstTableView.mj_footer  endRefreshing];
        [self.newstTableView.mj_header endRefreshing];
        //刷新UI
        [self.newstTableView  reloadData];
    }];
}
//加载最热列表的数据
- (void)loadHotTableViewDataSourceWithStart:(NSInteger)start  limit:(NSInteger)limit{
    [RequestManager fetchDataWithURL:kTopicUrl parameters:@{@"start":@(start),@"limit":@(limit),@"sort":@"hot"} method:@"POST" complet:^(NSData *data) {
        //
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            TopicModel  *model = [[TopicModel alloc] init];
            [model setValuesForKeysWithDictionary:smallDic];
            [model setValuesForKeysWithDictionary:smallDic[@"counterList"]];
            [self.hotDataSource addObject:model];
            
        }
        
       
   
    } updataUI:^{
        [self.hotTableView.mj_footer  endRefreshing];
        [self.hotTableView.mj_header endRefreshing];
        [self.hotTableView reloadData];
    }];
    
    
    
}

#pragma mark ------------     TableViewDelegate    -------------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.newstTableView) {
        return self.newstDataSource.count;
    }else if (tableView == self.hotTableView){
        return self.hotDataSource.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    TopicCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"topic_cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    TopicModel *model = [[TopicModel alloc] init];
    if (tableView ==self.newstTableView) {
          model = self.newstDataSource[indexPath.row];
    }else if (tableView == self.hotTableView){
        model = self.hotDataSource[indexPath.row];
    }

    [cell  setCellWithModel:model];
    
    cell.btnBlock = ^(){
        TopicListView *view = [[TopicListView alloc] init];
        view.contentID = model.contentid;
        [self.navigationController  pushViewController:view animated:YES];
    };
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.scrollView == scrollView) {
        if (self.scrollView.contentOffset.x == 0) {
            self.seg.selectedSegmentIndex = 0;
        }else if (self.scrollView.contentOffset.x == kwidth){
            self.seg.selectedSegmentIndex = 1;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

- (void)clickSegementController:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex == 0) {
        [self.scrollView setContentOffset:CGPointZero animated:YES ];
    }else{
        [self.scrollView setContentOffset:CGPointMake(kwidth, 0) animated:YES];
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    TopicListViewController *topicVC = [[TopicListViewController alloc] init];
    if (tableView == self.hotTableView) {
        TopicListModel *model = self.hotDataSource[indexPath.row];
        topicVC.contentId = model.contentid;
        topicVC.model_one_cell = self.hotDataSource[indexPath.row];
    }else{
        TopicModel *model = self.newstDataSource[indexPath.row];
        topicVC.contentId = model.contentid;
        topicVC.model_one_cell = self.newstDataSource[indexPath.row];
    }
    
    [self.navigationController pushViewController:topicVC animated:YES];


}

@end
